package com.hydl.power.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hydl.power.mapper.IdenticalMapper;
import com.hydl.power.mapper.SysUserMapper;
import com.hydl.power.pojo.Identical;
import com.hydl.power.pojo.Project;
import com.hydl.power.pojo.SysUser;
import com.hydl.power.service.IdenticalService;
import com.hydl.power.utils.Resp;
import com.hydl.power.utils.TableUtils;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wxb
 * @date 2019/12/31 14:41
 */
@Service
public class IdenticalServiceImpl implements IdenticalService {

    @Autowired
    private IdenticalMapper identicalMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public TableUtils getAllByPage(Integer pageNum, Integer pageSize) {
        Page page = PageHelper.startPage(pageNum,pageSize);
        List<Identical> list = identicalMapper.selectAll();
        TableUtils tableUtils = new TableUtils();
        tableUtils.setTotal(page.getTotal());
        tableUtils.setRows(list);
        return tableUtils;
    }

    @Override
    public List<Identical> getList() {
        return identicalMapper.selectAll();
    }
    /*
    * 查询同比分析数据
    * */

    @Override
    public PageInfo<Identical> queryIdentical(Integer pageNum,Integer pageSize,String YTD, String cityId, String projectId, String token,String startTime) {
        if (StringUtils.isBlank(startTime)){
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            startTime  = simpleDateFormat.format(new Date());
        }
        DecimalFormat df = new DecimalFormat("0.00%");
        SysUser sysUser = sysUserMapper.getUserByToken(token);
        if(sysUser!=null ){
            if(sysUser.getStatus() == 2 ){
                List<Identical> list = new ArrayList<>();
                if(sysUser.getProjectId()==null){
                     return new PageInfo<>(list);
                }

                if(YTD.equals("day") || YTD=="day"){
                    list =identicalMapper.queryIdenticalDay(pageNum,pageSize,cityId,sysUser.getProjectId().toString(),startTime);
                }else if(YTD.equals("month") || YTD=="month"){
                    list =identicalMapper.queryIdenticalMonth(pageNum,pageSize,cityId,sysUser.getProjectId().toString(),startTime.replaceAll("[[\\s-:punct:]]",""));
                }else {
                    list =identicalMapper.queryIdentical(pageNum,pageSize,cityId,sysUser.getProjectId().toString(),startTime);
                }
                if(!list.isEmpty()){
                    for (Identical identical : list) {
                        if(identical.getUpValue() == null){
                            identical.setUpValue(0);
                        }
                        if(identical.getUpValue()==0){
                          identical.setPercent("100%");
                        } else {
                          double percent=(identical.getCurValue()-identical.getUpValue());
                          percent=percent/identical.getUpValue();
                          identical.setPercent(df.format(percent));
                        }
                    }
                    return new PageInfo<>(list);
                }
            }
        }
        List<Identical>arr = new ArrayList<>();
        if(YTD.equals("day") || YTD=="day"){
            arr =identicalMapper.queryIdenticalDay(pageNum,pageSize,cityId,projectId,startTime);
        }else if(YTD.equals("month") || YTD=="month"){
            arr =identicalMapper.queryIdenticalMonth(pageNum,pageSize,cityId,projectId,startTime.replaceAll("[[\\s-:punct:]]",""));
        }else {
            arr =identicalMapper.queryIdentical(pageNum,pageSize,cityId,projectId,startTime);
        }
            for (Identical identical : arr) {
                if(identical.getUpValue() == null){
                    identical.setUpValue(0);
                }
              if(identical.getUpValue()==0){
                identical.setPercent("100%");
              } else {
                double percent=(identical.getCurValue()-identical.getUpValue());
                percent=percent/identical.getUpValue();
                identical.setPercent(df.format(percent));
              }
            }
            return  new PageInfo<>(arr);
    }
}
